using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Text;
using Castle.ActiveRecord;
using NHibernate.Expression;

namespace WOODIFramework.Model
{
    [ActiveRecord("Page"), JoinedBase]
    public class Page : ActiveRecordBase<Page>
    {
        #region Fields

        private Guid _id;
        private String _name;
        private int _numberColumns;
        private int _numberRows;
        private IList _items = new ArrayList();
        private object[,] _pageItemsAsTable;

        #endregion

        #region Properties

        [PrimaryKey(PrimaryKeyType.Assigned)]
        public Guid Id
        {
            get { return _id; }
            set { _id = value; }
        }

        [Property]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        [Property]
        public int NumberRows
        {
            get { return _numberRows; }
            set { _numberRows = value; }
        }

        [Property]
        public int NumberColumns
        {
            get { return _numberColumns; }
            set { _numberColumns = value; }
        }

        [HasAndBelongsToMany(typeof(PageItem),
        Table = "PageItemPage", ColumnKey = "PageId", ColumnRef = "PageItemId")]
        public IList Items
        {
            get { return _items; }
            set { _items = value; }
        }


        public object[,] PageItemsAsTable
        {
            get
            {
                if (_pageItemsAsTable == null)
                {
                    _pageItemsAsTable = new object[this.NumberColumns, this.NumberRows];

                    foreach(PageItem item in this.Items)
                    {
                        if (_pageItemsAsTable[item.ColumnPosition,item.RowPosition] == null)
                        {
                               _pageItemsAsTable[item.ColumnPosition,item.RowPosition] = new List<Object>();
                        }
                        ((List<Object>)_pageItemsAsTable[item.ColumnPosition, item.RowPosition]).Add(item);
                    }                    
                }
                return _pageItemsAsTable;
            }
        }

        #endregion

        #region Public Methods

        #region Public Static Methods
        /*
        public static Page CreatePage()
        {
            return new Page();
        }

        public static Page CreatePage(string Name)
        {
            return new Page(Name);
        }
        */
        public static Page FindPageByPageName(string Name)
        {
            ICriterion criteria = Expression.Eq("Name", Name);
            Page page = Page.FindFirst(criteria);
            return page;
        }
        
        #endregion

        #endregion


    }
}
